<?php

class HotelPhoto extends CActiveRecord
{
	public $photo;
	
	public function tableName()
	{
		return lfirst(__CLASS__);
	}
	
	public static function model($className = __CLASS__)
    {
    	return parent::model(lfirst(__CLASS__));
    }

    public function rules()
    {
        return array(
            array('photo', 'file', 'types'=>'jpeg, jpg, gif, png')
        );
    }
    
	public function relations()
	{
		return array(
	        'hotelPhotos' => array(self::BELONGS_TO, 'Hotel', 'id')
	    );
	}
	
	public function criteria($group = 'hotelId', $sort = 'id', $order = 'asc', $start = false, $limit = false)
	{
		$params = array();
		$thisTableName = $this->tableName();
		
		$params['group'] = $group;
		$params['order'] = $sort . ' ' . $order;
    	
    	if ($start !== false) {
    		$params['offset'] = intval($start);
    	}
    	
    	if ($limit !== false) {
    		$params['limit'] = intval($limit);
    	}
    	
		$this->getDbCriteria()->mergeWith($params);
    	
    	return $this;
	}
	
	public function getData($hotelIds = array())
	{
		$photos = null;
		
		if (count($hotelIds) > 0) {
			$criteria = new CDbCriteria();
			$criteria->condition = 'hotelId in (' . implode(',', $hotelIds) . ')';
			
			$photos = self::model()->criteria()->findAll($criteria);
		} else {
			$photos = self::model()->criteria()->findAll();
		}
		
		$photos = CActiveRecordData::getArray($photos);
		
		$tmpPhotoArr = array();
		if (count($photos) > 0) {
			foreach ($photos as $photo) {
				$tmpPhotoArr[$photo['hotelId']] = $photo['image']; 
			}
		}
		
		return $tmpPhotoArr;
	}
}